package com.lby.graduation.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lby.graduation.entity.Goods;
import com.lby.graduation.mapper.GoodsMapper;
import com.lby.graduation.service.GoodsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李兵耀
 * @since 2020-12-02
 */
@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsMapper goodsMapper;

    /*显示全部已发布商品信息*/
    @GetMapping("/showGoodsMessage")
    public List<Goods> showGoodsMessage() {
        return goodsService.showGoodsMessage();
    }

    /*分页查询*/
    @GetMapping("/selectPageGoods")
    public Map selectPageGoods(Integer current, Integer size) {
        Page<Goods> page = new Page<Goods>(current, size);
        HashMap<String, Object> map = new HashMap<>();
        List<Goods> goods = goodsMapper.selectPageGoods(page);
        long total = page.getTotal();
        map.put("goods", goods);
        map.put("count", total);
        map.put("code", 200);
        map.put("msg", "查找成功");
        return map;

    }

    /*随机查询三条商品数据*/
    @GetMapping("/GoodsMessageRand")
    public List<Goods> GoodsMessageRand() {
        return goodsService.GoodsMessageRand();
    }

    /*最新发布*/
    @GetMapping("/NewGoodsMessage")
    public List<Goods> NewGoodsMessage() {
        return goodsService.NewGoodsMessage();
    }


    /*用户已发布商品信息*/
    @GetMapping("/selectUserGoods")
    public List<Goods> selectUserGoods(Integer userId) {
        return goodsService.selectUserGoods(userId);
    }

    /*商品详情页信息*/
    @GetMapping("/selectGoodsByGoodsId")
    public List<Goods> selectGoodsByGoodsId(Integer goodsId) {
        return goodsService.selectGoodsByGoodsId(goodsId);

    }

    /*模糊查询*/
    @GetMapping("/findGoodsByKeyword")
    public List<Goods> findGoodsByKeyword(String type, String goodsName) {
        return goodsService.findGoodsByKeyword(type, goodsName);
    }

    /*插入商品*/
    @GetMapping("/insert")
    public boolean insert(Goods goods) {
        System.out.println("商品信息：" + goods);
        return goodsService.save(goods);
    }

    /*根据Id更新求购信息*/
    @GetMapping("/update")
    public boolean update(Goods goods) {
        return goodsService.updateById(goods);
    }

    /*单个删除*/
    @PostMapping("/deletes")
    public boolean deletes(Integer id) {
        return goodsService.removeById(id);
    }

    /*admin后台*/
    /*批量删除*/
    @RequestMapping("/deleteGoods")
    public Map deleteGoods(String ids) {
        HashMap<String, Object> map = new HashMap<>();
        JSONArray array = JSON.parseArray(ids);//传过来的json字符串转换成json数组
        List<Integer> idList = new ArrayList<>();
        for (int i = 0; i < array.size(); i++)//遍历将取出的值存到集合
        {
            String str = array.get(i).toString();
            idList.add(Integer.valueOf(str));
        }
        boolean b = goodsService.removeByIds(idList);
        if (b = true) {
            map.put("code", 200);
            map.put("msg", "删除成功");
            map.put("idList",idList);
        } else {
            map.put("msg", "删除失败");
        }
        return map;
    }

}
